public class LeetCode303_2 {
    // sum[i]存储前i个元素的和，sum[0] = 0
    // sum[i]存储nums[0...i-1]的和
    private int[] sum;
    /**
     * 创建一个数组，数组元素为另一个数组相同索引之前的元素和
     * @param nums
     */
    public LeetCode303_2(int[] nums) {
        sum = new int[nums.length + 1];
        sum[0] = 0;
        for (int i = 1; i < sum.length; i++) {
            sum[i] = sum[i - 1] + nums[i - 1];
        }
    }

    public int sumRange(int left, int right) {
        return sum[right+1] - sum[left];
    }


/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(left,right);
 */
}
